package entity

import (
	"encoding/json"
	"kop/modules/achievement/configuration/a_type"
	"time"
)

type Achievement struct {
	RoleID    int32       `gorm:"type:int;primary_key"`
	AType     a_type.Type `gorm:"type:smallint;primary_key"`
	Value     int64
	IDsData   []byte `gorm:"type:jsonb"`
	CreatedAt time.Time
	UpdatedAt time.Time

	IDs []int32 `sql:"-"`
}

func (e *Achievement) AfterFind() error {

	if e.IDs == nil {
		e.IDs = make([]int32, 0)
	}

	return json.Unmarshal(e.IDsData, &e.IDs)
}

func (e *Achievement) BeforeSave() (err error) {

	if e.IDs == nil {
		e.IDs = make([]int32, 0)
	}

	e.IDsData, err = json.Marshal(e.IDs)
	return err
}

func (e *Achievement) BeforeUpdate() (err error) {

	if e.IDs == nil {
		e.IDs = make([]int32, 0)
	}

	e.IDsData, err = json.Marshal(e.IDs)
	return err
}
